package com.itxiaowu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itxiaowu.entity.PageResult;
import com.itxiaowu.entity.QueryPageBean;
import com.itxiaowu.entity.Result;
import com.itxiaowu.pojo.Role;
import com.itxiaowu.service.DyRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/dyRole")
public class DyRoleController {

    @Reference
    private DyRoleService dyRoleService;

    //分页
    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = dyRoleService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()
                , queryPageBean.getQueryString());
        return pageResult;
    }

    //查询所有的权限信息和菜单信息
    @RequestMapping("/findAllPermissionMenu")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findAllPermissionMenu() {
        try {
            Map<String, Object> map = dyRoleService.findAllPermissionMenu();
            Result result = new Result(true, "查询权限信息和菜单信息成功", map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询权限信息和菜单信息失败!!!");
        }
    }

    //添加角色表添加权限和菜单数据
    @RequestMapping("/addPermission.do")
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    public Result addRolePermissionMenu(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            //都交给服务层处理
            dyRoleService.addRolePermissionMenu(role, permissionIds, menuIds);
            Result result = new Result(true, "角色添加成功");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色添加失败");
        }
    }

    //根据角色id获取角色数据
    @RequestMapping("/queryRoleById.do")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result queryRoleById(Integer id) {
        try {
            Role role = dyRoleService.queryRoleById(id);
            Result result = new Result(true, "角色查询成功", role);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色查询失败");
        }
    }

    //根据角色id查询选中的所有权限
    @RequestMapping("/findpermissionIdsById.do")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findpermissionIdsById(Integer id) {
        try {
            Integer[] permissionIds = dyRoleService.findPermissionIdsById(id);
            Result result = new Result(true, "角色所有权限id查询成功", permissionIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色所有权限id查询失败");
        }
    }

    //根据角色id查询选中的所有菜单
    @RequestMapping("/findMenuIdsById.do")
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    public Result findMenuIdsById(Integer id) {
        try {
            Integer[] permissionIds = dyRoleService.findMenuIdsById(id);
            Result result = new Result(true, "角色所有菜单id查询成功", permissionIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色所有菜单id查询失败");
        }
    }

    //修改角色信息
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    public Result edit(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            dyRoleService.edit(role, permissionIds, menuIds);
            Result result = new Result(true, "角色信息修改成功!!!");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色修改失败");
        }
    }

    //根据角色id删除角色
    @RequestMapping("/deleteRoleById")
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    public Result deleteRoleById(Integer id) {
        try {
            dyRoleService.deleteRoleById(id);
            Result result = new Result(true, "角色删除成功!!");
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色删除失败!!!!");
        }


    }
}